package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.DynamicRelayDetailBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

/**
 * Created by magikliu on 2017/9/28.
 */

public class DynamicRelayPraiseBaseAdapter extends BaseAdapter {

    private Context mContext;
    private List<DynamicRelayDetailBean.DataBean.UserThingBean> mList;
    private LayoutInflater inflater;
    private RequestOptions requestOptions;

    public DynamicRelayPraiseBaseAdapter(Context mContext, List<DynamicRelayDetailBean.DataBean.UserThingBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
        inflater = LayoutInflater.from(mContext);
        requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.default_avatar)
                .centerCrop()
                .override(100, 100);
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null){
            view = inflater.inflate(R.layout.item_dynamic_praise, null);
            viewHolder = new ViewHolder(view);
            Glide.with(mContext)
                    .load(mList.get(i).getPhoto() == null ? R.mipmap.default_avatar
                            : HttpRequestConstant.USER_PIC_HEAD + mList.get(i).getPhoto())
                    .apply(requestOptions)
                    .into(viewHolder.praiseAvatar);
            viewHolder.praiseName.setText(mList.get(i).getName());
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
            Glide.with(mContext)
                    .load(mList.get(i).getPhoto() == null ? R.mipmap.default_avatar
                            : HttpRequestConstant.USER_PIC_HEAD + mList.get(i).getPhoto())
                    .apply(requestOptions)
                    .into(viewHolder.praiseAvatar);
            viewHolder.praiseName.setText(mList.get(i).getName());
        }
        return view;
    }

    class ViewHolder{

        CircleImageView praiseAvatar;
        TextView praiseName;

        ViewHolder(View view){
            praiseAvatar = (CircleImageView) view.findViewById(R.id.praise_avatar);
            praiseName = (TextView) view.findViewById(R.id.praise_name);
        }
    }
}
